package com.it.myd.service.main.module.world;

import com.it.myd.bean.box.world.Item;
import com.it.myd.bean.box.world.Material;
import com.it.myd.bean.main.User;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class MaterialServiceImpl implements MaterialService{
    private Map<String, List<Material>> matelist = new HashMap<>();
    @Override
    public void setMaterial(Map<String, List<Material>> matelist) {
        this.matelist = matelist;
    }
    @Override
    public Map<String, List<Material>> getMaterial() {
        return matelist;
    }

    public Boolean chickMaterial(User user, Item item){
        List<Material> list = this.matelist.get(String.valueOf(user.getUser_id()));
        boolean flag = true;
        if(list != null){
            List<Material> needlist = item.getNeedMaters();
            for (Material material: needlist) {
                boolean f = false;
                material.setCount(String.valueOf(Integer.valueOf(material.getCount())*item.getCount()));
                for (Material mater: list) {
                    if(mater.getId() == material.getId()){
                        if (Integer.valueOf(material.getCount()) > Integer.valueOf(mater.getCount()) ) {
                            flag = false;
                            break;
                        }else{
                            f = true;
                        }
                    }
                    if(!flag){
                        break;
                    }
                }
                if(!f){
                    flag = false;
                    break;
                }
            }
        }else{
            flag = false;
        }
        if(flag){
            List<Material> needlist = item.getNeedMaters();
            for (Material material: needlist) {
                for (Material mater: list) {
                    if(mater.getId() == material.getId()){
                        mater.setCount(String.valueOf(Integer.valueOf(mater.getCount())-Integer.valueOf(material.getCount())));
                        break;
                    }
                }
            }
        }
        return flag;
    }
    @Override
    public void addMaterial(User user, Material mate) {
        List<Material> list = this.matelist.get(String.valueOf(user.getUser_id()));
        if(list != null){
            boolean f = false;
            for (Material material: list) {
                if(material.getId().equals(mate.getId())){
                    material.setCount(String.valueOf(Integer.parseInt(mate.getCount())+Integer.parseInt(material.getCount())));
                    f = true;
                    break;
                }
            }
            if(!f){
                list.add(mate);
            }
        }else{
            List<Material> addMatelist = new ArrayList<>();
            addMatelist.add(mate);
            this.matelist.put(String.valueOf(user.getUser_id()),addMatelist);
        }
    }

    @Override
    public void addMaterialList(User user, List<Material> matelist) {
        boolean flag = false;
        List<Material> list = this.matelist.get(user.getUser_id());
        List<Material> addMatelist = new ArrayList<>();
        if(list != null){
            for (Material mat: matelist) {
                boolean f = false;
                for (Material material: list) {
                    if(mat.getId().equals(material.getId())){
                        material.setCount(String.valueOf(Integer.parseInt(mat.getCount())+Integer.parseInt(material.getCount())));
                        f = true;
                        continue;
                    }
                }
                if(f){
                    continue;
                }else{
                    addMatelist.add(mat);
                }
            }
            if(addMatelist.size() > 0){
                for (Material material: addMatelist) {
                    list.add(material);
                }
            }
        }else{
            this.matelist.put(String.valueOf(user.getUser_id()),matelist);
        }
    }


}
